package impl;

import java.util.Random;

import services.BlocService;
import services.TerrainService;
import utils.Tresor;
import utils.Type;
import contracts.BlocContract;
import contracts.ContractException;

public class TerrainImpl implements TerrainService{
	private BlocService[][] blocs;
	private int nbColonnes;
	private int nbLignes;
	
	@Override
	public int getNombreColonnes() {
		return this.nbColonnes;
	}
	@Override
	public int getNombreLignes() {
		return this.nbLignes;
	}
	
	@Override
	public BlocService getBloc(int colonne, int ligne) throws ContractException {
		return this.blocs[ligne][colonne];
	}
	@Override
	public void init(int nbColonnes, int nbLignes) throws ContractException {
	
		this.nbColonnes = nbColonnes;
		this.nbLignes = nbLignes;
		blocs = new BlocService[this.nbLignes][this.nbColonnes];
		
		 Random r = new Random();
         for(int i = 0; i < nbLignes;  i++){
                 blocs[i] = new BlocService[nbColonnes];
                 for(int j = 0; j < nbColonnes; j++){
                         BlocService b =  new BlocContract(new BlocImpl());
                         if(i == 0 || j == 0 || i == (nbLignes-1) || j==(nbLignes-1) || i%2==0 && j%2==0){
                                 b.init(Type.MURMETAL, Tresor.RIEN);
                                 blocs[i][j] = b;
                         }
                         else if((i == 1 && j == 1) || (i == (nbLignes-2) && j == (nbColonnes-2))) {
                                 b.init(Type.VIDE, Tresor.RIEN);
                                 blocs[i][j] = b;
                         }
                         else if(r.nextInt(3) == 0) {
                                 int tr = r.nextInt(6); 
                                 if(tr==0)
                                         b.init(Type.MURBRIQUE, Tresor.BOMBPASS);
                                 else if(tr==1)
                                         b.init(Type.MURBRIQUE, Tresor.BOMBUP);
                                 else if(tr==2)
                                         b.init(Type.MURBRIQUE, Tresor.FIRESUIT);
                                 else if(tr==3)
                                         b.init(Type.MURBRIQUE, Tresor.FIREUP);
                                 else if(tr==4)
                                         b.init(Type.MURBRIQUE, Tresor.WALLPASS);
                                 else
                                         b.init(Type.MURBRIQUE, Tresor.RIEN);
                                 blocs[i][j] = b;
                         }
                         else {
                                 b.init(Type.VIDE, Tresor.RIEN);
                                 blocs[i][j] = b;
                         }

                 }
         }
		
			
	}
	
	
}
